#include "core/enhanced_chat_system.h"
#include <iostream>
#include <thread>
#include <chrono>

void printSeparator(const std::string& title) {
    std::cout << "\n" << std::string(50, '=') << std::endl;
    std::cout << "  " << title << std::endl;
    std::cout << std::string(50, '=') << std::endl;
}

void demonstratePrivateChat(EnhancedChatSystem& chatSystem) {
    printSeparator("私聊 (P2P) 演示");
    
    // 模拟用户登录
    chatSystem.addUser("Alice", 1001, "192.168.1.101");
    chatSystem.addUser("Bob", 1002, "192.168.1.102");
    
    std::cout << "✅ Alice 和 Bob 已上线" << std::endl;
    
    // 开始私聊
    std::string chatId = chatSystem.startPrivateChat("Alice", "Bob");
    std::cout << "📱 创建私聊会话: " << chatId << std::endl;
    
    // Alice 发送消息给 Bob
    chatSystem.sendPrivateMessage("Alice", "Bob", "Hi Bob, 你好吗？");
    std::cout << "💬 Alice -> Bob: Hi Bob, 你好吗？" << std::endl;
    
    // Bob 回复 Alice
    chatSystem.sendPrivateMessage("Bob", "Alice", "我很好，谢谢！你呢？");
    std::cout << "💬 Bob -> Alice: 我很好，谢谢！你呢？" << std::endl;
    
    // 查看聊天记录
    auto chat = chatSystem.getPrivateChat("Alice", "Bob");
    if (chat) {
        std::cout << "\n📋 聊天记录 (" << chat->messages.size() << " 条消息):" << std::endl;
        for (const auto& msg : chat->messages) {
            std::cout << "  [" << msg.fromUser << "] " << msg.content << std::endl;
        }
    }
    
    std::cout << "\n🔍 私聊特点:" << std::endl;
    std::cout << "  • 点对点通信，只有两个参与者" << std::endl;
    std::cout << "  • 消息只在两个用户间传递" << std::endl;
    std::cout << "  • 聊天ID基于用户名生成 (Alice_Bob)" << std::endl;
    std::cout << "  • 消息历史保存在会话中" << std::endl;
}

void demonstrateGroupChat(EnhancedChatSystem& chatSystem) {
    printSeparator("群聊 (Group) 演示");
    
    // 模拟更多用户上线
    chatSystem.addUser("Charlie", 1003, "192.168.1.103");
    chatSystem.addUser("David", 1004, "192.168.1.104");
    
    std::cout << "✅ Charlie 和 David 已上线" << std::endl;
    
    // Alice 创建群组
    std::string groupId = chatSystem.createGroup("技术讨论群", "Alice", "讨论技术问题的群组");
    std::cout << "🏠 Alice 创建群组: 技术讨论群 (ID: " << groupId << ")" << std::endl;
    
    // 其他用户加入群组
    chatSystem.joinGroup(groupId, "Bob");
    chatSystem.joinGroup(groupId, "Charlie");
    chatSystem.joinGroup(groupId, "David");
    
    std::cout << "👥 Bob, Charlie, David 已加入群组" << std::endl;
    
    // 群组信息
    auto group = chatSystem.getGroup(groupId);
    if (group) {
        std::cout << "📊 群组信息:" << std::endl;
        std::cout << "  • 群名: " << group->groupName << std::endl;
        std::cout << "  • 群主: " << group->ownerId << std::endl;
        std::cout << "  • 成员数: " << group->members.size() << std::endl;
        std::cout << "  • 描述: " << group->description << std::endl;
    }
    
    // 群聊消息
    chatSystem.sendGroupMessage(groupId, "Alice", "欢迎大家加入技术讨论群！");
    chatSystem.sendGroupMessage(groupId, "Bob", "谢谢群主！");
    chatSystem.sendGroupMessage(groupId, "Charlie", "大家好！");
    chatSystem.sendGroupMessage(groupId, "David", "很高兴认识大家");
    
    std::cout << "\n💬 群聊消息:" << std::endl;
    std::cout << "  Alice: 欢迎大家加入技术讨论群！ → 广播给 3 人" << std::endl;
    std::cout << "  Bob: 谢谢群主！ → 广播给 3 人" << std::endl;
    std::cout << "  Charlie: 大家好！ → 广播给 3 人" << std::endl;
    std::cout << "  David: 很高兴认识大家 → 广播给 3 人" << std::endl;
    
    // 查看群消息历史
    if (group) {
        std::cout << "\n📋 群消息记录 (" << group->messages.size() << " 条消息):" << std::endl;
        for (const auto& msg : group->messages) {
            std::cout << "  [" << msg.fromUser << "] " << msg.content << std::endl;
        }
    }
    
    std::cout << "\n🔍 群聊特点:" << std::endl;
    std::cout << "  • 一对多通信，支持多个参与者" << std::endl;
    std::cout << "  • 消息广播给所有群成员" << std::endl;
    std::cout << "  • 有群主和管理员概念" << std::endl;
    std::cout << "  • 支持邀请、加入、离开机制" << std::endl;
    std::cout << "  • 群消息历史保存在群组中" << std::endl;
}

void demonstratePerformanceDifference() {
    printSeparator("性能对比分析");
    
    std::cout << "📊 消息传递复杂度对比:" << std::endl;
    std::cout << "┌────────────┬──────────┬──────────┬──────────┐" << std::endl;
    std::cout << "│    模式    │  时间复杂度  │  空间复杂度  │  网络开销  │" << std::endl;
    std::cout << "├────────────┼──────────┼──────────┼──────────┤" << std::endl;
    std::cout << "│   私聊     │   O(1)   │   O(1)   │    1x    │" << std::endl;
    std::cout << "│   群聊     │   O(n)   │   O(n)   │    nx    │" << std::endl;
    std::cout << "│ 全局广播   │   O(n)   │   O(n)   │    nx    │" << std::endl;
    std::cout << "└────────────┴──────────┴──────────┴──────────┘" << std::endl;
    
    std::cout << "\n🚀 性能特点:" << std::endl;
    std::cout << "私聊 (P2P):" << std::endl;
    std::cout << "  ✅ 消息传递最快，O(1) 时间复杂度" << std::endl;
    std::cout << "  ✅ 网络开销最小，只发送一次" << std::endl;
    std::cout << "  ✅ 服务器压力小" << std::endl;
    std::cout << "  ❌ 无法多人协作" << std::endl;
    
    std::cout << "\n群聊 (Group):" << std::endl;
    std::cout << "  ✅ 支持多人协作交流" << std::endl;
    std::cout << "  ✅ 有权限管理和组织结构" << std::endl;
    std::cout << "  ❌ 消息需要广播给 n 个用户" << std::endl;
    std::cout << "  ❌ 网络开销随成员数增长" << std::endl;
    
    std::cout << "\n全局广播 (当前系统):" << std::endl;
    std::cout << "  ❌ 消息发送给所有在线用户" << std::endl;
    std::cout << "  ❌ 无隐私保护" << std::endl;
    std::cout << "  ❌ 网络开销最大" << std::endl;
    std::cout << "  ❌ 无法精确控制接收者" << std::endl;
}

void demonstrateScenarios() {
    printSeparator("使用场景对比");
    
    std::cout << "🎯 私聊适用场景:" << std::endl;
    std::cout << "  • 一对一客服咨询" << std::endl;
    std::cout << "  • 私密敏感信息交流" << std::endl;
    std::cout << "  • 商务谈判对话" << std::endl;
    std::cout << "  • 朋友间的私人聊天" << std::endl;
    std::cout << "  • 医生患者咨询" << std::endl;
    
    std::cout << "\n🎯 群聊适用场景:" << std::endl;
    std::cout << "  • 团队协作讨论" << std::endl;
    std::cout << "  • 项目进度汇报" << std::endl;
    std::cout << "  • 技术交流群" << std::endl;
    std::cout << "  • 兴趣爱好社群" << std::endl;
    std::cout << "  • 公司部门群组" << std::endl;
    
    std::cout << "\n🎯 全局广播适用场景:" << std::endl;
    std::cout << "  • 系统公告通知" << std::endl;
    std::cout << "  • 紧急事件通报" << std::endl;
    std::cout << "  • 简单的聊天室" << std::endl;
    std::cout << "  • 游戏内全服喊话" << std::endl;
}

int main() {
    std::cout << "🚀 聊天系统架构对比演示" << std::endl;
    
    EnhancedChatSystem chatSystem;
    
    // 演示私聊
    demonstratePrivateChat(chatSystem);
    
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    
    // 演示群聊
    demonstrateGroupChat(chatSystem);
    
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    
    // 性能对比
    demonstratePerformanceDifference();
    
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    
    // 使用场景对比
    demonstrateScenarios();
    
    printSeparator("总结");
    std::cout << "📝 关键差异总结:" << std::endl;
    std::cout << "1. 📡 消息路由: 私聊是点对点，群聊是一对多广播" << std::endl;
    std::cout << "2. 👥 参与者: 私聊固定2人，群聊动态多人" << std::endl;
    std::cout << "3. 🔐 权限控制: 私聊无权限，群聊有管理员机制" << std::endl;
    std::cout << "4. 💾 存储结构: 私聊按对话存储，群聊按群组存储" << std::endl;
    std::cout << "5. 🚀 性能特征: 私聊O(1)，群聊O(n)" << std::endl;
    std::cout << "6. 🎯 应用场景: 私聊适合隐私，群聊适合协作" << std::endl;
    
    return 0;
} 